package com.example.beat_x3.Adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beat_x3.Client.GetResult;
import com.example.beat_x3.Client.LoadImages;
import com.example.beat_x3.R;

import java.util.ArrayList;

import Entity.Inf;
import Entity.MyTime;
import Entity.PlaceSchedule;

public class PlaceScheduleAdapter extends RecyclerView.Adapter<PlaceScheduleAdapter.ViewHolder> {
    public PlaceSchedule placeSchedule;
    public String placeNumber;
    public String placeImageSrc;

    public PlaceScheduleAdapter(String placeNumber, String placeImageSrc) {
        this.placeImageSrc = placeImageSrc;
        this.placeNumber = placeNumber;
    }

    @NonNull
    @Override
    public PlaceScheduleAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_reservation, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PlaceScheduleAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.date.setText(placeSchedule.Schedule.get(position).PlaceDate);
        holder.image.setImageURI(Uri.parse(LoadImages.PLACE_SRC + "/" + placeImageSrc));
        ArrayList<MyTime> timeTable = placeSchedule.Schedule.get(position).TimeTable;
        TimeAdapter timeAdapter = new TimeAdapter(timeTable);
        timeAdapter.setOnItemClickListener(new TimeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String placeTimeNumber, String startTime, boolean status, ImageView image, int position1) {
                if (status && Inf.isLog) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Inf.context);
                    View viewInflated = LayoutInflater.from(Inf.context).inflate(R.layout.dialogue_reservation, null, false);
                    EditText input = viewInflated.findViewById(R.id.input);
                    builder.setTitle(R.string.app_name);
                    String appointmentTime = placeSchedule.Schedule.get(position).PlaceDate + " " + startTime;
                    builder.setMessage("您的预约时间是:" + appointmentTime);
                    builder.setView(viewInflated);
                    builder.setPositiveButton("预定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    String res = GetResult.Reserve(placeTimeNumber, Inf.stuInf.StudentAccount, input.getText().toString().trim(), appointmentTime);
                                    Looper.prepare();
                                    if (res.equals("1")) {
                                        Toast.makeText(Inf.context, "您已预约，可查看预约进度", Toast.LENGTH_SHORT).show();
                                        image.setImageResource(R.drawable.red);
                                        timeTable.get(position1).Status = false;
                                    } else {
                                        Toast.makeText(Inf.context, "预约失败", Toast.LENGTH_SHORT).show();
                                    }
                                    Looper.loop();
                                }
                            }).start();
                        }
                    });
                    builder.setNegativeButton("Cancel", null);
                    AlertDialog alert = builder.create();
                    alert.show();
                } else {
                    Toast.makeText(Inf.context, "请登录", Toast.LENGTH_SHORT).show();
                }
            }
        });
        holder.recyclerView.setAdapter(timeAdapter);
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(Inf.context));
    }

    @Override
    public int getItemCount() {
        if (placeSchedule == null) return 0;
        return placeSchedule.Schedule.size();
    }
    @SuppressLint("NotifyDataSetChanged")
    public void refresh(){
        notifyDataSetChanged();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView date;
        RecyclerView recyclerView;
        ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            date = itemView.findViewById(R.id.re_textView);
            recyclerView = itemView.findViewById(R.id.time_recyclerView1);
            image = itemView.findViewById(R.id.re_image);
        }
    }
}